// 结构化数据标记
function addSchemaMarkup() {
    const script = document.createElement('script');
    script.type = 'application/ld+json';
    script.text = JSON.stringify({
        "@context": "https://schema.org",
        "@type": "WebSite",
        "name": "仓鼠の官网",
        "url": "https://yzycoc.com",
        "description": "仓鼠的个人官网 - 一枚程序猿的网站，记录软件学习生涯的学习历程和作品",
        "potentialAction": {
            "@type": "SearchAction",
            "target": "https://yzycoc.com/blog?q={search_term_string}",
            "query-input": "required name=search_term_string"
        }
    });
    document.head.appendChild(script);
    
    // 添加面包屑导航
    const breadcrumbScript = document.createElement('script');
    breadcrumbScript.type = 'application/ld+json';
    breadcrumbScript.text = JSON.stringify({
        "@context": "https://schema.org",
        "@type": "BreadcrumbList",
        "itemListElement": [{
            "@type": "ListItem",
            "position": 1,
            "name": "首页",
            "item": "https://yzycoc.com"
        }]
    });
    document.head.appendChild(breadcrumbScript);
}

// 预加载关键资源
function preloadCriticalResources() {
    const resources = [
        'hamster-coding.svg',
        'project-bg1.jpg',
        'project-bg2.jpg',
        'project-bg3.jpg',
        'code-lines.svg'
    ];
    
    resources.forEach(resource => {
        const link = document.createElement('link');
        link.rel = 'preload';
        link.href = resource;
        link.as = resource.endsWith('.svg') ? 'image' : 'image';
        document.head.appendChild(link);
    });
}

// 添加canonical链接
function addCanonicalLink() {
    const link = document.createElement('link');
    link.rel = 'canonical';
    link.href = window.location.href.split('?')[0].split('#')[0];
    document.head.appendChild(link);
}

// 初始化SEO功能
document.addEventListener('DOMContentLoaded', function() {
    addSchemaMarkup();
    preloadCriticalResources();
    addCanonicalLink();
    
    // 跟踪页面浏览
    if(typeof gtag !== 'undefined') {
        gtag('config', 'GA_MEASUREMENT_ID', {
            'page_title': document.title,
            'page_path': window.location.pathname
        });
    }
});